# création de plots montrant l'association d'individus aux composantes en fonction de l'étude desquels ils sont tirés
plot.indiv.components.orig <- function(nb_comp=NULL, all_famfile=NULL, cases_controls_famfile=NULL, controls_famfiles=NULL, phenofile="pheno_strat.txt",outputfile="qqplot_components_orig"){
	
	if(!is.null(nb_comp) & !is.null(all_famfile) & !is.null(cases_controls_famfile) & !is.null(controls_famfiles)){

		# chargement des donnees
		all <- read.table(file="../all.fam")
		for (i in 1:(length(controls_famfiles))){
			tmp <- read.table(file=controls_famfiles[i])
			if(i==1){ all$orig <- ifelse(all$V2 %in% tmp$V2,i+2,NA) }
			else{ all$orig <- ifelse(all$V2 %in% tmp$V2,i+2,all$orig)}
		}
		study <- read.table(file=cases_controls_famfile)
		all$orig <- ifelse((all$V2 %in% study$V2) & (all$V6==1),1,all$orig) 
		all$orig <- ifelse((all$V2 %in% study$V2) & (all$V6==2),2,all$orig) 
		
		strat <- read.table(file=phenofile, h=T)
		col_names <- colnames(strat[-c(1,2)])
		strat <- merge(strat, all, by.x="IID", by.y="V2")
		strat <- strat[-c(1,2)] 
		colnames(strat) <- NULL
		
		nb_plot <- ((nb_comp)*(nb_comp -1))/2
		nb_page <- ifelse(nb_plot%%4 == 0, nb_plot%/%4, (nb_plot%/%4)+1)
		nb_output <- 1 #compte le nombre de fichiers en sortie
		png_bool <- 0 # permet la gestion du nombre de fichiers png (en fonction du nombre de plots déjà mis sur le png)
		last_plot <-0

		for(indA in 1:(nb_comp -1) ){
			for(indB in (indA+1):nb_comp){
				compA <- strat[c(indA)]
				compB <- strat[c(indB)]
				col <- strat[c(length(strat))]
				# creation des parametres de la legende 
				names <- c("controls","cases",controls_famfiles)
				col_legend<- cbind(c(1,2),names)

				# creation de la matrice de donnees servant au plot
				tmp <- cbind(compA,compB,col)	
				if(nb_page > 1){
					if(png_bool == 0){png(paste(outputfile,nb_output,".png",sep=""), width=860, height=860); par(mfrow=c(2,2))}#on ouvre un nouveau png tous les 4 plots
					# creation du plot
					plot(tmp$compA, tmp$compB, main="plot components", xlab=col_names[indA], ylab=col_names[indB],col=tmp$col)
					legend("bottomright",paste(col_legend[,1]," : ",col_legend[,2],sep=""),pch=1,col = 1:dim(col_legend)[1])
					png_bool <- png_bool +1
					last_plot <- last_plot +1

					if(png_bool == 4){
						dev.off()
						cat("see : ",outputfile,nb_output,".png\n")
						png_bool <- 0
						nb_page <- nb_page -1
						nb_output <- nb_output +1
					}
				}
				else{
					if(png_bool == 0){png(paste(outputfile,nb_output,".png",sep=""),  width=860, height=860); par(mfrow=c(2,2))}

					# creation du plot
					plot(tmp$compA, tmp$compB, main="plot components", xlab=col_names[indA], ylab=col_names[indB],col=tmp$col)
					legend("bottomright",paste(col_legend[,1]," : ",col_legend[,2],sep=""),pch=1, col = 1:dim(col_legend)[1])
					png_bool <- png_bool +1
					last_plot <- last_plot +1

					if(last_plot == nb_plot){
						dev.off()
						cat("see : ",outputfile,nb_output,".png\n")
						png_bool <- 0
						nb_page <- nb_page -1
					}
				}
			}
		}
	}
}



## changer le status pour colorer le graph par étude
#all <- read.table(file="forStrat3.fam")
#oba <- read.table(file="oba_lille_controls.fam")
#canada <- read.table(file="canada_300k_controls.fam")
#pvm <- read.table(file="pvmcont_new.fam")

# all$orig <- ifelse(all$V2 %in% oba$V2,1,NA) 
# all$orig <- ifelse(all$V2 %in% canada$V2,2,all$orig) 
# all$orig <- ifelse((all$V2 %in% pvm$V2) & (all$V6==1),4,all$orig) 
# all$orig <- ifelse((all$V2 %in% pvm$V2) & (all$V6==2),5,all$orig) 
#table(all$orig)

# création de plots montrant l'association d'individus aux composantes
plot.indiv.components <- function(nb_comp=NULL, famfile="forStrat2.fam", phenofile="pheno_strat.txt", famfiles_for_origin=NULL ,outputfile="qqplot_components", color="status"){# color=sex for coloring by sex, color=status for coloring cases/controls
#famfiles_for_origin=c("forStrat5.fam","../oba_lille_controls.fam","../canada_300k_controls.fam","../pvmcont_new.fam")

	if(!is.null(nb_comp)){
	
			# chargement des donnees
			fam <- read.table(file=famfile)
			strat <- read.table(file=phenofile, h=T)
			col_names <- colnames(strat[-c(1,2)])
			strat <- merge(strat, fam, by.x="IID", by.y="V2")
			strat <- strat[-c(1,2)] 
			colnames(strat) <- NULL

		nb_plot <- ((nb_comp)*(nb_comp -1))/2
		nb_page <- ifelse(nb_plot%%4 == 0, nb_plot%/%4, (nb_plot%/%4)+1)
		nb_output <- 1 #compte le nombre de fichiers en sortie
		png_bool <- 0 # permet la gestion du nombre de fichiers png (en fonction du nombre de plots déjà mis sur le png)
		last_plot <-0

		for(indA in 1:(nb_comp -1) ){
			for(indB in (indA+1):nb_comp){
				compA <- strat[c(indA)]
				compB <- strat[c(indB)]
				if(color=="sex") {
					col <- strat[c(length(strat)-1)]
					# creation des parametres de la legende 
					col_legend<- cbind(c(1,2),c("male","female"))
				}
				if(color=="status"){
					col <- strat[c(length(strat))]
					col_legend<- cbind(c(1,2),c("controls","cases"))
				}
				# creation de la matrice de donnees servant au plot
				tmp <- cbind(compA,compB,col)	
				if(nb_page > 1){
					if(png_bool == 0){png(paste(outputfile,nb_output,".png",sep=""), width=860, height=860); par(mfrow=c(2,2))}#on ouvre un nouveau png tous les 4 plots
					# creation du plot
					plot(tmp$compA, tmp$compB, main="plot components", xlab=col_names[indA], ylab=col_names[indB],col=tmp$col)
					legend("bottomright",paste(col_legend[,1]," : ",col_legend[,2],sep=""),pch=1,col = 1:dim(col_legend)[1])
					png_bool <- png_bool +1
					last_plot <- last_plot +1

					if(png_bool == 4){
						dev.off()
						cat("see : ",outputfile,nb_output,".png\n")
						png_bool <- 0
						nb_page <- nb_page -1
						nb_output <- nb_output +1
					}
				}
				else{
					if(png_bool == 0){png(paste(outputfile,nb_output,".png",sep=""),  width=860, height=860); par(mfrow=c(2,2))}

					# creation du plot
					plot(tmp$compA, tmp$compB, main="plot components", xlab=col_names[indA], ylab=col_names[indB],col=tmp$col)
					legend("bottomright",paste(col_legend[,1]," : ",col_legend[,2],sep=""),pch=1,col = 1:dim(col_legend)[1])
					png_bool <- png_bool +1
					last_plot <- last_plot +1

					if(last_plot == nb_plot){
						dev.off()
						cat("see : ",outputfile,nb_output,".png\n")
						png_bool <- 0
						nb_page <- nb_page -1
					}
				}
			}
		}
	}
}




